<template>
  <div class="app-container">
    <div class="change">
      <p>
        <span>领用仓库</span>
        <el-select v-model="tempData.cangku" :disabled="islook" @change="changeCangku" placeholder="请选择领用仓库" class="userselect">
          <el-option
            v-for="item in cangku"
            :key="item.sid"
            :label="item.names"
            :value="item.names"
          ></el-option>
        </el-select>
      </p>
      <p>
        <span>业务日期</span>
        <el-date-picker v-model="tempData.yewutime" :disabled="islook" type="date" value-format="yyyy-MM-dd" class="userselect" placeholder="业务日期"></el-date-picker>
      </p>
      <p>
        <span>领用公司</span>
        <el-select v-model="tempData.gongsi" :disabled="islook" placeholder="请选择领用公司" class="userselect">
          <el-option
            v-for="item in gongsi"
            :key="item.sid"
            :label="item.names"
            :value="item.names"
          ></el-option>
        </el-select>
      </p>
      <p>
        <span>领用部门</span>
        <el-select v-model="tempData.departments" :disabled="islook" placeholder="请选择领用部门" class="userselect">
          <el-option
            v-for="item in ifydata"
            :key="item.id"
            :label="item.departments"
            :value="item.departments"
          ></el-option>
        </el-select>
      </p>
      <p>
        <span>领用人</span>
        <el-input placeholder="请输入领用人" :disabled="islook" v-model="tempData.lingyongren" class="userselect">
          <el-button slot="append" icon="el-icon-s-custom" :disabled="islook" @click="checkBuyer1"></el-button>
        </el-input>
      </p>
      <p>
        <span>经办人</span>
        <el-input v-model="tempData.persons" disabled type="text" class="userselect" placeholder="请输入经办人" />
      </p>
      <p>
        <span>经办日期</span>
        <el-input v-model="tempData.personTime" disabled type="text" class="userselect" placeholder="经办日期"></el-input>
      </p>
      <p>
        <span>领用备注</span>
        <el-input v-model="tempData.remarks" :disabled="islook" type="textarea" class="userselect" placeholder="请输入出库备注" />
      </p>
    </div>
    <!-- 产品数据 -->
    <div class="zd3-btm">
      <div class="addDate" @click="addPeople" v-show="!islook">+ 选择耗材</div>
      <el-table :data="proData" :show-header="true" style="width: 100%;border-radius: 8px;">
        <el-table-column type="index" label="序号" align="center" width="80" />
        <el-table-column align="center" label="照片" width="150">
				  <template slot-scope="scope">
				    <img :src="scope.row.icon" alt="" class="avatar">
				  </template>
				</el-table-column>
				<el-table-column align="center" label="物品编码" prop="bianma" width="150"></el-table-column>
        <el-table-column align="center" label="物品名称" prop="names" width="150"></el-table-column>
        <el-table-column align="center" label="规格型号" prop="guige" width="150"></el-table-column>
        <el-table-column align="center" label="单位" prop="danwei"></el-table-column>
        <el-table-column align="center" label="安全库存上限" prop="anquanUp" width="150"></el-table-column>
        <el-table-column align="center" label="安全库存下限" prop="anquanDowm" width="150"></el-table-column>
        <el-table-column align="center" label="备注" prop="remarks" width="150"></el-table-column>
        <el-table-column align="center" label="出库数量" width="150">
          <template slot-scope="scope">
            <input v-model="scope.row.tempnum" v-on:input="changeNum(scope.row)" :class="scope.row.ischeck ? 'inps-active' : 'inps'"  type="text" :disabled="!scope.row.ischeck"/>
          </template>
        </el-table-column>
        <el-table-column align="center" label="单价" width="150" prop="unitPrice">
          <template slot-scope="scope">
            <input v-model="scope.row.unitPrice" :class="scope.row.ischeck ? 'inps-active' : 'inps'"  type="text" :disabled="!scope.row.ischeck"/>
          </template>
        </el-table-column>
        <el-table-column align="center" label="出库金额" width="150">
          <template slot-scope="scope">
            <input v-model="scope.row.numJine = scope.row.unitPrice * scope.row.tempnum" :class="scope.row.ischeck ? 'inps-active' : 'inps'"  type="text" :disabled="!scope.row.ischeck"/>
          </template>
        </el-table-column>
        <el-table-column align="center" width="150" label="操作" fixed="right" v-if="!islook">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="save(scope.row,scope.$index)" v-if="scope.row.ischeck">保存</el-button>
            <el-button type="text" size="small" @click="write(scope.row,scope.$index)" v-else>修改</el-button>
            <el-popconfirm
              placement="top"
              hideIcon
              title="是否要删除此行？"
              @onConfirm="deleteRow(scope.row,scope.$index)"
            >
              <el-button type="text" size="small" slot="reference">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
			<div class="submits">
        <el-button type="primary" v-if="islook" size="small" @click="nosaveOrder">返回</el-button>
        <el-button type="primary" v-else v-loading="isClick" size="small" @click="saveOrder">保存</el-button>
        <el-button type="info" size="small" @click="nosaveOrder">取消</el-button>
			</div>
    </div>
		<!-- 新增信息 -->
		<el-dialog title="选择耗材" :visible.sync="addProShow" width="85%" top="20px" :close-on-click-modal="false">
		  <div class="product">
        <div class="pro-l">
          <el-tree
            :data="twoClassify"
            node-key="names"
            highlight-current
            :props="{label: 'names',children:'list'}"
            @current-change="handleNodeClick"
          ></el-tree>
        </div>
        <div class="pro-r">
          <el-table
            ref="singleTable"
            :data="proDeail"
            border
            highlight-current-row
            @current-change="handleAdd"
            style="width: 100%"
            height="570"
          >
            <el-table-column align="center" label="照片">
              <template slot-scope="scope">
                <img :src="scope.row.imgurl" alt="" class="avatar">
              </template>
            </el-table-column>
            <el-table-column align="center" prop="status" width="100" label="资产状态" fixed="left">
              <template slot-scope="scope">
                <div class="orderStatus">
                  <p v-if="scope.row.status === '闲置'" class="yellow">闲置</p>
                  <p v-if="scope.row.status === '报废'" class="red">报废</p>
                  <p v-if="scope.row.status === '借用'" class="blue">借用</p>
                  <p v-if="scope.row.status === '使用中'" class="green">使用中</p>
                </div>
              </template>
            </el-table-column>
            <el-table-column align="center" width="150" prop="bianma" label="物品编码"/>
            <el-table-column align="center" prop="names" label="物品名称"/>
            <el-table-column align="center" prop="propertyType" label="商品条码"/>
            <el-table-column align="center" prop="guige" label="规格型号"/>
            <el-table-column align="center" prop="danwei" label="单位" />
            <el-table-column align="center" prop="unitPrice" label="单价" />
            <el-table-column align="center" width="120" prop="anquanUp" label="安全库存上限" />
            <el-table-column align="center" width="120" prop="anquanDowm" label="安全库存下限" />
            <el-table-column align="center" label="库存" prop="num" width="100"></el-table-column>
            <el-table-column align="center" prop="remarks" label="备注" />
          </el-table>
          <el-pagination
            background
            align="right"
            layout="prev, pager, next"
            :page-count="totalAssets"
            @current-change='goPageAssets'
            @prev-click="goPrevAssets"
            @next-click="goNextAssets"
          />
        </div>
		  </div>
		</el-dialog>
    <el-dialog title="领用人" :visible.sync="buyerShow1" width="80%" top="20px" :close-on-click-modal="false">
      <div class="files">
				<div class="files-left">
					<el-menu class="el-menu-vertical-demo" @select="handleSelece" default-active="0">
						<el-menu-item :index='index' v-for="(item,index) in ifydata" :key="index">
							<span slot="title">{{item.departments}}</span>
						</el-menu-item>
					</el-menu>
				</div>
				<div class="files-right">
					<div class="files-btm">
						<el-table 
              :data="peopleData" 
              height="400" 
              highlight-current-row
              @current-change="handlePeople1"
              style="margin-bottom: 20px;" 
              :show-header="true" >
							<el-table-column align="center" prop="username" label="姓名"></el-table-column>
							<el-table-column align="center" prop="departments" label="所属部门"></el-table-column>
							<el-table-column align="center" prop="sex" label="性别"></el-table-column>
							<el-table-column align="center" prop="power" label="职务"></el-table-column>
						</el-table>
						<el-pagination background hide-on-single-page :page-size="50" layout="prev, pager, next" :total="total" @prev-click="goPrev" @current-change='goPage' @next-click="goNext" />
					</div>
				</div>
			</div>
    </el-dialog>
  </div>
</template>
<script>
import { haocaiLingTui,addHaocaiRuku,updateHaocaiRuku,selectHaocaiFenlei,selectHaocaiZichan,getGongying,getSeting,haocaiLingTuiZichan } from '@/api/caigou'
import { getUser,getBumenNew } from '@/api/people'
let selectid = null,navindex = null;
export default {
  data() {
    return {
      radio: "1",
			total: 1,
      page: 1,
      totalAssets: 1,
      pageAssets: 1,
      value: '',
      tableData: [],
      oneClassify: [],   //一级分类
      twoClassify: [],   //商品分类
      gongsi: [],  //公司列表
      tempData: {},
			cangku: [{
				label: '仓库1',
				value: 1
			},
			{
				label: '仓库2',
				value: 2
      }],
			proDeail: [],  //商品列表
      currentRow: null,
      gongying: [],  //供应商
      peopleData: [],  //人员列表
      ifydata:[],
      buyerShow1: false, //人员弹窗状态
      addProShow: false, //商品信息弹窗
      cangkusearch: '',  //仓库搜索
      proData: [],
      isClick: false,
			islook: false,   //是否是查看操作
      writOradd: 0 //0新增，1编辑
    };
  },
  mounted(){
    if(this.$route.params.options){
      this.tempData = JSON.parse(this.$route.params.options)
      console.trace('tempData:',this.tempData)
      this.proData = this.tempData.lists
      this.writOradd = 1
    }else{
      this.writOradd = 0
    }
    console.log(this.$store.state.user.userInfo)
    this.tempData.persons = this.$store.state.user.userInfo[0].username
    // this.tempData.departments = this.$store.state.user.userInfo[0].departments
		if(this.$route.params.look){
			this.islook = true
			console.log(this.islook)
		}else{
      const userinfo = this.$store.state.user.userInfo[0]
      this.getpeople(userinfo.departments)
      this.bumenList()
      this.getAsset()
      this.getClassify()
      this.getSet()
      console.log(this.$route.params)
    }
    this.getDate()
  },
  methods: {
    changeNum(row){
      console.log(row)
      if(row.tempnum*1 > row.num*1){
        this.$message({
          message: '库存不足',
          type: 'warning'
        })
        row.tempnum = row.num
      }
    },
    // 选择出库仓库
    changeCangku(arg){
      console.log(arg)
      this.cangkusearch = arg
      this.getAsset()
    },
    getDate(){
      let myDate = new Date()
      this.tempData.personTime = `${myDate.getFullYear()}-${myDate.getMonth()+1}-${myDate.getDate()}`
    },
    // 部门列表
    bumenList() {
      getBumenNew({}).then((res) => {
        console.log("----------------");
        console.log(res);
        let temp = res.data.filter(item => item.ids === '456')[0].departmentsList
        console.log(temp)
        temp.map((item) => {
          item.value = item.departments;
          return item;
        });
        this.ifydata = temp;
      });
    },
    /**
     * 点击分类菜单获取对应商品
     */
    handleNodeClick(data) {
      console.log(data)
      this.getAsset(data.ids)
    },
    getAsset(type) {
      selectHaocaiZichan({parentId: type ? type : null,cangk: this.cangkusearch ? this.cangkusearch : null,currentPage: this.pageAssets}).then((res) => {
        console.log(res);
        let temparr = res.data.list
        this.totalAssets = res.data.pages
        this.proDeail = temparr
      });
    },
    /**
     * 获取资产分类
     */
    getClassify(){
      selectHaocaiFenlei({}).then(res => {
        console.log('资产分类',res)
        this.twoClassify = res.data
      })
    },
    // 点击分类菜单获取对应商品
    handleNodeClick(data, checked, node) {
      console.log(data)
      selectid = data.ids
      this.getAsset(data.ids)
    },
    getSet(){
      getSeting({}).then(res => {
        console.log('分类',res)
        // 供应商
        this.gongying = res.data.filter(item => item.status === '7')
        // 仓库
        this.cangku = res.data.filter(item => item.status === '3')
        // 公司
        this.gongsi = res.data.filter(item => item.status === '1')
        console.log(this.gongying)
      })
    },
    // 选择人员分类
    handleSelece(index){
			console.log(index)
			console.log('ifydata:',this.ifydata[index])
			navindex = this.ifydata[index].value ? this.ifydata[index].value : null
			this.getpeople(navindex)
    },
    // 人员列表
    getpeople(id){
      console.log('id:',id)
      getUser({departments: id,currentPage: this.page}).then(res => {
        console.log('部门人员',res)
        this.peopleData = res.data.list
        this.total = res.data.total
      })
    },
    // 修改商品信息
		write(value,index){
			console.log(index)
			this.proData.map(item => item.ischeck = false)
			let obj = this.proData[index];
			obj.ischeck = true;
			this.$set(this.proData, index, obj);
			// this.proData[index].ischeck = true
			// console.log(this.proData[index].ischeck)
		},
		// 保存修改操作
		save(value,index){
			let obj = this.proData[index];
			obj.ischeck = false;
			this.$set(this.proData, index, obj);
			// this.proData[index].ischeck = false
    },
    checkBuyer1() {
      this.buyerShow1 = true;
    },
    // 选择联系人
    handlePeople1(row){
      console.log(row)
      this.tempData.lingyongren = row.username
			this.buyerShow1 = false
    },
		// 增加商品
    addPeople() {
      if(this.tempData.cangku){
        this.addProShow = true
      }else{
        this.$message({
          message: '请先选择领用得仓库',
          type: 'warning'
        })
      }
      
		},
    // 资产分页
    goPrevAssets(page) {
      console.log(page)
      this.pageAssets--
      this.getAsset(selectid)
    },
    goNextAssets(page) {
      console.log(page)
      this.pageAssets++
      this.getAsset(selectid)
    },
    goPageAssets(page){
      console.log(page)
      this.pageAssets = page
      this.getAsset(selectid)
    },
    // 分页
    goPrev(page) {
      console.log(page)
      this.page--
      this.getpeople(navindex)
    },
    goNext(page) {
      console.log(page)
      this.page++
      this.getpeople(navindex)
    },
    goPage(page){
      console.log(page)
      this.page = page
      this.getpeople(navindex)
    },
		// 确认选择供应商
    handleGongying(val) {
      console.log(val);
      this.currentRow = val.name;
    },
		// 选择供应商
    checkGy() {
      this.gyShow = true;
    },
		// 选择采购员
    checkBuyer() {
      this.buyerShow = true;
    },
		// 选择商品列表增加商品
		handleAdd(row){
			console.log(row)
			row.ischeck = false
      row.tempnum = 1
      let index = this.proData.findIndex(item => item.churu === row.cid)
      row.churu = row.cid
      if(index == -1){
        this.proData.push({...row})
        console.log(this.proData)
        this.addProShow = false
      }else{
        this.$message({
          message: '该资产已选择！',
          type: 'warning'
        })
      }
    },
    // 选择操作人员
    handlePeople(row){
      console.log(row)
      this.tempData.operator = row.username
      console.log(this.tempData.buyer)
			this.buyerShow = false
		},
		// 删除对应商品数据
		deleteRow(value,index){
			console.log(index)
			this.proData.splice(index,1)
		},
		// 保存新增入库单
		saveOrder(){
      if(!this.proData.length){
        console.log('meiyouxuanze')
        this.$message({
          message: '请选择耗材！',
          type: 'warning'
        })
        return
      }
      console.log('保存')
      this.isClick = true
      this.proData.map(item => {
        delete item.updateTime
        delete item.createTime
        delete item.insertTime
        item.num = item.tempnum
        item.status = '领用'
        item.departments = this.tempData.departments
        return item
      })
      this.tempData.ptype = '未确认'
      this.tempData.ptypes = '1'
      this.tempData.lists = this.proData
      if(this.writOradd == 0){
        addHaocaiRuku(this.tempData).then(res => {
          console.log(res)
          this.$message({
            message: res.message,
            type: 'success'
          })
          this.$router.go(-1)
          this.isClick = false
        })
      }
      
		},
		// 取消新增
		nosaveOrder(){
			console.log('取消')
			this.$router.go(-1)
		}
  }
};
</script>
<style lang="scss" scoped>
/deep/.el-input-group__append button.el-button{
  padding: 8px 12px;
}
.orderStatus {
  display: flex;
	justify-content: center;
  > p {
    padding: 0px 5px;
    color: #fff;
    font-size: 12px;
    border-radius: 2px;
    margin: 0 0px 0 0;
  }
  .blue {
    background: #3a87ad;
  }
  .green {
    background: #18a05e;
  }
  .yellow {
    background: #ff7a0e;
  }
  .red {
    background: #c60000;
  }
  .zise{
    background: #c7ab10
  }
}
/deep/.el-button--primary {
  color: #fff;
  background-color: #18a05e !important;
  border-color: #18a05e !important;
}
/deep/.el-button--primary:hover,
.el-button--primary:focus {
  background: #18a05e !important;
  border-color: #18a05e !important;
  color: #fff !important;
}
/deep/.el-button:hover,
.el-button:focus {
  color: #18a05e;
  background-color: transparent;
}
/deep/.el-button--primary {
  color: #fff;
  background-color: #18a05e;
  border-color: #18a05e;
}
.popup {
  padding: 0 20px;
}
.product{
  display: flex;
  .pro-l{
    width: 200px;
  }
  .pro-r{
    flex: 1;
    overflow: hidden;
    /deep/.el-table--enable-row-hover .el-table__body tr:hover>td {
      background-color: rgba(24,160,94,.2);
    }
    /deep/.el-table td{
      padding: 5px 0;
    }
    /deep/.el-table tr{
      cursor: pointer;
    }
    /deep/.el-table__body tr.current-row>td {
      background-color: rgba(24,160,94,.4);
    }
  }
}
.files {
  display: flex;
  background: #fff;
  border: 1px solid rgba(0, 0, 0, 0.1);
  border-radius: 8px;
  margin-top: 20px;
  overflow: hidden;
  padding: 20px;
}
/deep/.el-pagination.is-background .el-pager li:not(.disabled).active {
  background-color: #18a05e;
  color: #fff;
}
.files-btm /deep/.btn-prev:after {
  content: "上一页";
}
.files-btm /deep/.btn-next:before {
  content: "下一页";
}
.files-btm /deep/.btn-prev,
.files-btm /deep/.btn-next {
  width: 65px;
	display: inline-flex;
	align-items: center;
	justify-content: center;
}
.files-left {
	width: 160px;
	margin-right: 20px;
  height: 420px;
  overflow-y: auto;
	/deep/.el-menu{
		border-right: none;
	}
	/deep/.el-menu-item{
		height: 35px;
		line-height: 35px;
		margin-bottom: 8px;
	}
	/deep/.el-menu-item.is-active{
		color: #fff;
		border-radius: 5px;
		background: rgba(24,160,94,.8);
	}
}
.files-right {
  flex: 1;
  overflow: hidden;
}
.files-btm {
  background: rgba(255, 255, 255, 1);
  border-radius: 8px;
  border: 1px solid rgba(0, 0, 0, 0.1);
  // margin-top: 20px;
  overflow: hidden;
  &-title {
    display: flex;
    align-items: center;
    height: 45px;
    border-bottom: 1px solid rgba(0, 0, 0, 0.1);
    background: #ffffff;
    > p {
      text-align: center;
      font-size: 16px;
      color: #333;
    }
  }
  .addDate {
    margin: 20px 32px 10px 32px;
    height: 32px;
    border-radius: 3px;
    text-align: center;
    line-height: 32px;
    font-size: 14px;
    color: #333;
    cursor: pointer;
    border: 1px dashed rgba(0, 0, 0, 0.3);
  }
  .el-button--text {
    margin: 0 15px;
  }
}
.avatar{
	width: 30px;
	height: 30px;
}
.zd3-btm {
  background: rgba(255, 255, 255, 1);
  border-radius: 8px;
  border: 1px solid rgba(0, 0, 0, 0.1);
  margin-top: 10px;
  overflow: hidden;
  &-title {
    display: flex;
    align-items: center;
    height: 70px;
    border-bottom: 1px solid rgba(0, 0, 0, 0.1);
    background: #ffffff;
    > p {
      text-align: center;
      font-size: 16px;
      color: #333;
    }
  }
  .addDate {
    margin: 20px 32px 10px 32px;
    height: 32px;
    border-radius: 3px;
    text-align: center;
    line-height: 32px;
    font-size: 14px;
    color: #333;
    cursor: pointer;
    border: 1px dashed rgba(0, 0, 0, 0.3);
  }
  .el-button--text {
    margin: 0 15px;
  }
}
.inps {
  width: 95%;
  height: 32px;
  line-height: 32px;
  padding-left: 10px;
  background: none;
  border: none;
  color: rgba(0, 0, 0, 0.8);
}
.inps-active {
  width: 95%;
  height: 32px;
  line-height: 32px;
  padding-left: 10px;
  background: rgba(24,160,94,.2);
  border: none;
  color: rgba(0, 0, 0, 0.8);
}
.change {
  display: flex;
  flex-wrap: wrap;
  background: #fff;
  border-radius: 8px;
  padding: 20px;
  > p {
    width: 25%;
    font-size: 14px;
    display: flex;
    align-items: center;
    color: #333;
    /deep/.el-input__inner {
      // width: inherit;
      background: none;
      height: 32px;
      // line-height: 32px;
      color: #333;
      border: 1px solid rgba(0, 0, 0, 0.1);
      padding: 0 25px;
    }
    /deep/.el-input__icon {
      line-height: 32px;
    }
    .userselect {
      // max-width: 250px;
      flex: 1;
      display: flex;
      line-height: 30px;
    }

    > span {
      display: block;
      width: 120px;
      text-align: right;
      padding-right: 15px;
    }
    > input {
      width: inherit;
      flex: 1;
      height: 32px;
      border-radius: 3px;
      border: 1px solid rgba(0, 0, 0, 0.1);
      padding-left: 10px;
      color: #333;
    }
  }
}
.submits{
	display: flex;
	padding: 15px 20px;
	justify-content: flex-end;
	align-items: center;
	>div{
		width: 80px;
		height: 32px;
		text-align: center;
		line-height: 32px;
		color: #fff;
		font-size: 14px;
		border-radius: 4px;
		margin-left: 30px;
		cursor: pointer;
	}
	.save{
		background: #2196F3;
	}
	.nosave{
		background: #999;
	}
}
@media screen and (max-width: 1400px) {
  .change > p {
    width: 33%;
  }
}
</style>